/*
 * Copyright (C), 2007-2018, 铭师堂教育科技有限公司
 * FileName: AppInfoServiceImpl.java
 * Author:   zhaoyi
 * Date:     2018年5月2日 上午9:33:04
 * Description: //模块目的、功能描述      
 * History: //修改记录
 * <author>      <time>      <version>    <desc>
 * 修改人姓名             修改时间            版本号                  描述
 */
package com.secsbrain.cmdb.web.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.secsbrain.cmdb.biz.IAppSystemService;
import com.secsbrain.cmdb.biz.IHostService;
import com.secsbrain.common.utils.RedisUtil;
import com.secsbrain.cmdb.dal.model.Host;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RestController;

import com.secsbrain.common.ResponseVo;
import com.secsbrain.cmdb.client.iface.HostInfoService;
import com.secsbrain.cmdb.client.model.HostDetail;

/**
 * 主机信息api
 *
 * @author zhaoyi
 */
@RestController
@EnableEurekaClient
public class HostInfoServiceImpl implements HostInfoService{
	
    private static final Logger logger = LoggerFactory.getLogger(HostInfoServiceImpl.class);

	@Autowired
    IHostService hostService;
	
	@Autowired
    IAppSystemService appSystemService;

	/* (non-Javadoc)
	 * @see HostInfoService#queryHostDetailById(java.lang.Long)
	 */
	@Override
	public ResponseVo<HostDetail> queryHostDetailById(Long hostId) {
		Assert.notNull(hostId, "hostId不能为空");

		Host host=hostService.selectById(hostId);
		Assert.notNull(host, "没有查询到相关主机");

		return ResponseVo.getSuccessResponse(parseHostDetail(host));
	}

	/**
	 * 功能描述: <br>
	 * 转换主机对象
	 *
	 * @param host
	 * @return
	 */
	private HostDetail parseHostDetail(Host host) {
		HostDetail hostDetail=new HostDetail();
		hostDetail.setCpu(host.getNumCpus());
		hostDetail.setHost(host.getHost());
		hostDetail.setHostName(host.getHostName());
		hostDetail.setId(host.getId());
		hostDetail.setMemory(host.getMemTotal());
		hostDetail.setOs(host.getOs());
		hostDetail.setServerDesc(host.getComment());
		hostDetail.setServerIp(host.getIp());
		hostDetail.setCreateTime(host.getCreateTime());
		hostDetail.setUpdateTime(host.getUpdateTime());
		hostDetail.setEnvName(host.getEnv());
		hostDetail.setAppList(hostService.hostUseApp(host.getId()));
		return hostDetail;
	}

	/* (non-Javadoc)
	 * @see HostInfoService#queryAllHost()
	 */
	@Override
	public ResponseVo<List<HostDetail>> queryAllHost() {
		List<HostDetail> hostDetailList=null;

		String redisKey="host_list";
		if(RedisUtil.kExists(redisKey)){
			hostDetailList= JSON.parseArray(RedisUtil.get(redisKey), HostDetail.class);
		}else{
			hostDetailList=this.getHostList();
			RedisUtil.put(redisKey, JSON.toJSONString(hostDetailList),60*30);//缓存30分钟
		}
		return ResponseVo.getSuccessResponse(hostDetailList);
	}
	
	private List<HostDetail> getHostList(){
		logger.debug("从数据库读取主机信息。。。");
		Host hostParam=new Host();
		List<Host> hostList=hostService.selectList(hostParam);
		List<HostDetail> hostDetailList=new ArrayList<HostDetail>();
		for(Host host:hostList){
			hostDetailList.add(parseHostDetail(host));
		}
		return hostDetailList;
	}

	/* (non-Javadoc)
	 * @see HostInfoService#queryIpByHost(java.lang.String[])
	 */
	@Override
	public ResponseVo<Map<String, List<String>>> queryIpByHost(String[] hostList) {
		Assert.notEmpty(hostList, "主机名数组不能为空");
		return ResponseVo.getSuccessResponse(hostService.queryIpByHost(hostList));
	}

}
